package com.lanou3g.musicenjoy.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.SearchSinger;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by dllo on 15/10/22.
 */
public class SearchingSingerAdapter extends BaseAdapter {
    private Context mContext;
    private List<SearchSinger.DataEntity> data;

    public SearchingSingerAdapter(Context mContext) {
        this.mContext = mContext;
    }

    //设置数据
    public void setDatas(List<SearchSinger.DataEntity> data) {
        this.data = data;
        notifyDataSetInvalidated();
    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("xin", "88888888");
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.searching_singer_content, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SearchSinger.DataEntity entity = (SearchSinger.DataEntity) getItem(position);
        Log.i("xin", entity + "99999");
        if (entity != null) {
            holder.singerName.setText(entity.getSinger_name());
            holder.albumNum.setText(entity.getAlbum_num() + "");
            holder.songNum.setText(entity.getSong_num() + "");
            Log.i("xin", entity.getSinger_name() + "1111");
            Log.i("xin", entity.getAlbum_num() + "2222");
            Log.i("xin", entity.getSong_num() + "3333");
            Log.i("xin", entity.getPic_url() + "4444");
            if (entity.getPic_url() != null) {
                //加载图片
                Picasso.with(mContext).load(entity.getPic_url()).into(holder.singerCircle);
            }

        }
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'searching_singer_content.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.singer_circle)
        CircleImageView singerCircle;
        @Bind(R.id.singer_name)
        TextView singerName;
        @Bind(R.id.song_num)
        TextView songNum;
        @Bind(R.id.single_song)
        TextView singleSong;
        @Bind(R.id.album_num)
        TextView albumNum;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
